//
/* global _isWX */
(function() {
	App.set("$Cache.sign_in.name", Cookies.get("cache_user_login_name"));
	App.set("$Cache.sign_in.admin.name", Cookies.get("cache_bus_login_name"));
	var _user_login_pwd = Cookies.get("cache_user_login_pwd");
	if (_user_login_pwd) {
		App.set("$Cache.sign_in.password", _user_login_pwd);
		App.set("$State.sign_in.remenber_password", true);
	}
	var _bus_login_pwd = Cookies.get("cache_bus_login_pwd");
	if (_bus_login_pwd) {
		App.set("$Cache.sign_in.admin.password", _bus_login_pwd);
		App.set("$State.sign_in.admin.remenber_password", true);
	}
	if (App.get('loginer')) {
		Path.jump("/user/main.html");
	};
	Path.on('/sign_in.html', function(_current_location) {
		App.set('$State.sign_in.loginType', Path.getQuery("login_type") === "bus"); //默认用户（商家）

		App.set('$State.sign_in.signType', Path.getQuery("sign_type") === "sign_up"); //默认登录（注册）
	});
	App.set('$Event.sign_in.changeSignType', function() {
		Path.setQuery("sign_type", App.get('$State.sign_in.signType') ? 'sign_in' : 'sign_up');
		// App.set('$State.sign_in.signType', !App.get('$State.sign_in.signType'));
	});
	App.set('$Event.sign_in.changeLoginType', function() {
		Path.setQuery("login_type", App.get('$State.sign_in.loginType') ? 'user' : 'bus');
		// App.set('$State.sign_in.loginType', !App.get('$State.sign_in.loginType'));
	});
	var timer;
	App.set('$Event.sign_in.getcheckcode', function() {
		clearInterval(timer);
		var getcodetime = 60;
		App.set('$State.sign_in.remaining', getcodetime);
		App.set('$State.sign_in.getcode', true);
		timer = setInterval(function() {
			if (getcodetime > 0) {
				getcodetime -= 1;
				App.set('$State.sign_in.remaining', getcodetime);
			} else {
				clearInterval(timer);
				App.set('$State.sign_in.getcode', '');
			}
		}, 1000);
	});


	/*
	 *登录
	 */
	//获取一个验证码
	function _loadCodeImg() {
		// $code.val("");
		// $signInImg.prop("src",appConfig.user.sign_in_img_code_url+"?_="+Math.random());
	};
	// _loadCodeImg();
	// 一键购买标识
	var _gid = Path.getQuery("gid");
	if (_gid) {
		alert("warn", "您未登陆，请登陆后继续购买")
	};
	//用户登录
	App.set("$Event.sign_in.login", function() {
		var login_data = App.get("$Cache.sign_in");
		login_data.name = jSouper.$.trim(login_data.name);
		var PathJump = _isWX ? Path.wxJump : Path.jump;
		coAjax.post(appConfig.user.sign_in_url, login_data, function success(result) {
			alert("success", "登录成功");
			App.set("loginer", result.result);
			Cookies.set("cache_user_login_name", login_data.name);
			Cookies.set("cache_user_login_pwd", App.get("$State.sign_in.remenber_password") ? login_data.password : "");
			var callbackUrl = Path.getQuery("cb_url");
			if (_gid) {
				alert("正在将你刚才要买的商品加入订单中")
				require(["/js/common/goodsCart.js"], function(goodsCart) {
					goodsCart.add_to_cart(_gid, 1, function() {
						Path.jump("/cart.html");
					}, function() {
						Path.jump("/main.html");
					})
				});
				return;
			}

			eventManager.emitAfter("getLoginer");
			PathJump(callbackUrl || "/main.html");
		}, function error(errorMsg, errorCode, xhr) {
			console.log(errorMsg);
			switch (errorCode) {
				case "0001":
					alert("error", "密码输入错误");
					break;
				case "00033":
					alert("error", "“ " + login_data.name + " ”这个登录账号还没有注册，请检查您的用户名，或者先进行注册。");
					break;
				case "00034":
					alert("error", "您还未输入登录用的用户名");
					break;
				case "00035":
					alert("error", "您还未输入登录密码");
					break;
				case "00037":
					alert("error", "您还未输入验证码");
					return;
					break;
				case "00038":
					alert("error", "您输入验证码有些慢，为了您的安全，请重新输入");
					break;
				case "00039":
					alert("error", "输入的验证码错误");
					break;
				default:
					alert("error", errorMsg);
			}
			_loadCodeImg();
		});
	});


	/*
	 * 注册
	 */
	App.set("$Event.sign_up.register", function() {
		var register_data = App.get("$Cache.sign_up");
		register_data.confirm_password = register_data.password;
		coAjax.post(appConfig.user.sign_up_url, register_data, function(result) {
			alert("success", "注册成功");
			App.set("$Cache.sign_in.name", register_data.mobile_phone);
			App.set("$Cache.sign_in.password", register_data.password);
			App.get("$Event.sign_in.login")();
			//清空表单
			App.set("$Cache.sign_up", {});
			//切换到登录
			App.set('$Cache.sign_in.signPrivate', '');
		});
	});

	App.set("$Event.sign_up.get_register_code", function() {
		coAjax.get(appConfig.server_url + "user/registerCode", {
			mobile_phone: App.get("$Cache.sign_up.mobile_phone")
		}, function(result) {
			alert(result.result);
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
			App.set('$State.sign_in.getcode', '');
		});
	});

	// 卖家登陆
	App.set("$Event.sign_in.admin.login", function(argument) {
		var login_data = App.get("$Cache.sign_in.admin");
		var username = jSouper.$.trim(login_data.name);
		var password = login_data.password;
		coAjax.post(appConfig.bus.login, {
			name: username,
			password: password
		}, function() {
			alert("success", "商家登录成功");
			// var callbackUrl = Path.getQuery("cb_url");
			Cookies.set("cache_bus_login_name", username);
			Cookies.set("cache_bus_login_pwd", App.get("$State.sign_in.admin.remenber_password") ? password : "");
			//直接到商家后台
			Path.jump("http://admin.dotnar.com");
		});
	});
}());;

(function() {
	/* 切换到下一个输入框 */
	App.set("$Event.sign_in.admin.to_password_input", function() {
		App.set("$State.sign_in.admin.is_focus", true);
		App.set("$State.sign_in.admin.is_focus", false);
	});
	App.set("$Event.sign_in.user.to_password_input", function() {
		App.set("$State.sign_in.user.is_focus", true);
		App.set("$State.sign_in.user.is_focus", false);
	});
	/*显示、关闭用户协议*/
	App.set("$Event.sign_in.show_user_rules", function() {
		App.set("$State.sign_in.show_user_rules", true);
	});
	App.set("$Event.sign_in.close_user_rules", function() {
		App.set("$State.sign_in.show_user_rules", false);
	});
	/*图形验证码*/
	var _ti;

	function _phone_code_time() {
		clearInterval(_ti);
		var _s = 60;
		_ti = setInterval(function _() {
			_s -= 1;
			App.set("$Cache.sign_up.next_phone_code_time", _s);
			if (_s === 0) {
				clearInterval(_ti);
				App.set("$State.sign_up.waiting_server", false);
			}
			return _;
		}(), 1000);
	};
	App.set("$Event.sign_up.get_register_code_with_image_code", function() {
		var mobile_phone = jSouper.$.trim(App.get("$Cache.sign_up.mobile_phone") || "");
		if (!mobile_phone) {
			native_alert("请输入手机号码");
			return
		}
		App.set("$State.sign_up.waiting_user_input_phone_code", true);
		App.set("$State.sign_up.waiting_server", true);
		coAjax.get(appConfig.user.register_code_with_image_code + App.get("$Cache.sign_up.image_code_for_phone_code"), {
			mobile_phone: mobile_phone
		}, function(result) {
			alert("success", "验证码发送成功");
			_phone_code_time();
		}, function(errorMsg, errorCode, xhr) {
			alert("error", errorMsg);
			App.set("$State.sign_up.waiting_server", false);
			App.set("$State.sign_up.waiting_user_input_phone_code", false);
			//重新获取图形验证码
			App.get("$Event.sign_up.change_user_image_code")();
		});
	});
	App.set("$Event.sign_up.reset_register_code", function() {
		App.set("$State.sign_up.waiting_user_input_phone_code", false);
		App.get("$Event.sign_up.change_user_image_code")();
	});
	App.set("$Event.sign_up.change_user_image_code", function() {
		App.set("$Cache.sign_up.image_code_random", Math.random());
		App.set("$Cache.sign_up.image_code_for_phone_code", "");
	});
	/*检查新账号的数据可用性*/
	App.set("$Event.sign_up.checkedMobilePhone", function() {
		coAjax.get(appConfig.user.registerCheckedMobilePhone + App.get("$Cache.sign_up.mobile_phone"), function() {
			alert("success", "手机号码可用");
		});
	});
	App.set("$Event.sign_up.checkedEmail", function() {
		coAjax.get(appConfig.user.registerCheckedEmail + App.get("$Cache.sign_up.email"), function() {
			alert("success", "邮箱可用");
		});
	});
})();